package camara.prueba;

/*import android.app.Activity;
 import android.os.Bundle;

 public class SimpleCamera extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 }
 }*/

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SimpleCamera extends Activity implements SurfaceHolder.Callback {
	
	private Camera camera;
	private boolean isPreviewRunning = false;
	private SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Uri targetResource = Media.EXTERNAL_CONTENT_URI;

	
	public void onCreate(Bundle icicle) {
		
		super.onCreate(icicle);
		
		Log.e(getClass().getSimpleName(), "onCreate");
		
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.main);
		
		surfaceView = (SurfaceView) findViewById(R.id.surface);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	
	
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		
		MenuItem item = menu.add(0, 0, 0, "Ver Fotos");
		
		item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
		public boolean onMenuItemClick(MenuItem item) {
			Intent intent = new Intent(Intent.ACTION_VIEW, targetResource);
			startActivity(intent);
			return true;
		}
		});
		
		return true;
	}
	
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState){
		super.onRestoreInstanceState(savedInstanceState);
	}
	
	//se invoca al realizar una fotografía
	Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() { 
		
		//obtiene los datos de la imagen directamente de la cámara
		public void onPictureTaken(byte[] data, Camera c) {
				camera.startPreview();
		}
	};
	
	Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback(){

		@Override
		//se usa para reproducir un sonido, pero ahora mismo no lo usamos
		public void onShutter() {
			// TODO Auto-generated method stub
			
		} 
	};
	
	
	//comprueba si está liberada la tecla central del teclado, en caso afirmativo definimos
	//la creación de un archivo
	public boolean onKeyDown(int keyCode, KeyEvent event){
		ImageCaptureCallback camDemo = null;
		if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			try {
					String filename = timeStampFormat.format(new Date());
					ContentValues values = new ContentValues();
					values.put(Media.TITLE, filename);
		
					values.put(Media.DESCRIPTION, "Imagen capturada por la camara");
					Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
					camDemo = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
			} 
			catch(Exception ex ){
			}
		}
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return super.onKeyDown(keyCode, event);
		}
		
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			camera.takePicture(mShutterCallback, mPictureCallbackRaw, camDemo);
			return true;
		}
		return false;
	}
	
	protected void onResume(){
		super.onResume();
	}
	
	protected void onSaveInstanceState(Bundle outState){
		super.onSaveInstanceState(outState);
	}
	
	protected void onStop(){
		super.onStop();
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
		if (isPreviewRunning) {
			camera.stopPreview();
		}
		
		Camera.Parameters p = camera.getParameters();
		p.setPreviewSize(w, h);
		camera.setParameters(p);
		
		try {
			camera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		camera.startPreview();
		isPreviewRunning = true;
	}
	
	public void surfaceCreated(SurfaceHolder holder){
		camera = Camera.open();
	}
	
	public void surfaceDestroyed(SurfaceHolder holder){
		camera.stopPreview();
	
		isPreviewRunning = false;
		camera.release();
	}
	
}